package com.example.nonggo.view.act;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.example.nonggo.Constants;
import com.example.nonggo.R;
import com.example.nonggo.api.BaseApi;
import com.example.nonggo.api.MDataObserver;
import com.example.nonggo.bean.GoodsPriceDTO;
import com.example.nonggo.bean.VarietyMarketDetailRes;
import com.tencent.mmkv.MMKV;

public class VarietyPriceListActivity extends AppCompatActivity {


    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_variety_price_list);

        getWindow().setNavigationBarColor(Color.TRANSPARENT);
        // 隐藏状态栏
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        // 设置Back按钮的点击事件
        ImageButton backButton = findViewById(R.id.back_button);
        backButton.setOnClickListener(v -> this.onBackPressed());
        String varietyName = getIntent().getStringExtra("varietyName");
        getData(varietyName);
        TextView variety = findViewById(R.id.text_view_2_collapsing);
        variety.setText(String.format("品种：%s",varietyName));
    }

    private void getData(String varietyName) {
        String token = MMKV.defaultMMKV().getString(Constants.ACCESS_TOKEN, "0");
        RxHttpUtils.createApi(BaseApi.class)
                .priceList(token, new GoodsPriceDTO(varietyName).toRequestBody())
                .compose(Transformer.switchSchedulers(null))
                .subscribe(new MDataObserver<>() {
                    @Override
                    protected void onSuccess(VarietyMarketDetailRes varietyMarketDetailRes) {
                        LinearLayout view = findViewById(R.id.linearLayout);
                        int childCount = view.getChildCount();
                        // 移除多余的子视图
                        while (view.getChildCount() > 3){
                            view.removeViewAt(childCount - 1);
                        }
                        for (VarietyMarketDetailRes.VarietyMarketDetail detail : varietyMarketDetailRes.getList()) {


                            View itemView = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_variety_price, null);
                            TextView marketNameView = itemView.findViewById(R.id.market_name);
                            marketNameView.setText(String.format("批发市场：%s", detail.getMarketName()));
                            marketNameView.setSelected(true);

                            TextView marketPriceView = itemView.findViewById(R.id.market_price);
                            marketPriceView.setText(String.format("市场价：%s%s", detail.getMiddlePrice(), detail.getMeteringUnit()));

                            TextView priceMinView = itemView.findViewById(R.id.market_price_min);
                            priceMinView.setText(String.format("最低价：%s%s", detail.getMinimumPrice(),detail.getMeteringUnit()));

                            TextView priceAvgView = itemView.findViewById(R.id.market_price_average);
                            priceAvgView.setText(String.format("大宗价：%s%s", detail.getHighestPrice(),detail.getMeteringUnit()));

                            TextView averageAreaPriceView = itemView.findViewById(R.id.market_price_average_area);
                            averageAreaPriceView.setText(String.format("场地：%s", detail.getAreaName()));
                            view.addView(itemView);
                        }
                    }
                });
    }


}